package com.atguigu.flink.chapter05.sink;

import com.alibaba.fastjson.JSON;
import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.RuntimeContext;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.elasticsearch.ElasticsearchSinkFunction;
import org.apache.flink.streaming.connectors.elasticsearch.RequestIndexer;
import org.apache.flink.streaming.connectors.elasticsearch6.ElasticsearchSink;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.xcontent.XContentType;

import java.util.ArrayList;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/13 11:02
 */
public class Flink03_Es {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        DataStream<WaterSensor> stream = env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
            });
        
        ArrayList<HttpHost> hosts = new ArrayList<>();
        hosts.add(new HttpHost("hadoop162", 9200));
        hosts.add(new HttpHost("hadoop163", 9200));
        hosts.add(new HttpHost("hadoop164", 9200));
        
        ElasticsearchSink.Builder<WaterSensor> builder = new ElasticsearchSink.Builder<WaterSensor>(
            hosts,
            new ElasticsearchSinkFunction<WaterSensor>() {
                @Override
                public void process(WaterSensor element,
                                    RuntimeContext ctx,
                                    RequestIndexer indexer) {
                    // 实现这个方法, 来完成向es写数据
                    IndexRequest request = Requests
                        .indexRequest()
                        .index("sensor")
                        .type("_doc")
                        .id(element.getId())
                        .source(JSON.toJSONString(element), XContentType.JSON);
                    
                    indexer.add(request);
                    
                }
            }
        );
        // 设置刷数据到es的间隔, 默认没有值, 不会刷新
        builder.setBulkFlushInterval(1000);
        // 每来一条数据刷新一次
        builder.setBulkFlushMaxActions(1);
        
        builder.setBulkFlushMaxSizeMb(10);
        
        stream
            .keyBy(WaterSensor::getId)
            .sum("vc")
            .addSink(builder.build());
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
es

index    数据库

type      表
    5.x之前  一个index可以有多个type
    6.x开始  一个index只能有一个type
    7.x开始  去掉了type

doc      行

 */